package com.wut.clock.controller;

import com.wut.clock.aop.log.BusinessType;
import com.wut.clock.aop.log.Log;
import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.entity.Notification;
import com.wut.clock.service.INotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 系统通知管理
 * @date 2022/5/31 21:06
 */
@Slf4j
@Api(value="系统通知管理器",tags = {"系统通知管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/notification")
public class NotificationController {

    private final INotificationService notificationService;

    @ApiOperation("添加系统通知")
    @PostMapping("/addNotification")
    @Log(title = "添加系统通知",businessType = BusinessType.INSERT)
    @RequiresPermissions("am:notification:add")
    public CommonResult<String> addNotification(@RequestBody Notification notification){
        if(notificationService.addNotification(notification)>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除系统通知")
    @PostMapping("/deleteNotification/{notificationId}")
    @Log(title = "删除系统通知",businessType = BusinessType.DELETE)
    @RequiresPermissions("am:notification:delete")
    public CommonResult<String> deleteNotification(@NotNull @PathVariable Integer notificationId){
        if(notificationService.deleteNotification(notificationId)>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新系统通知")
    @PostMapping("/updateNotification")
    @Log(title = "更新系统通知",businessType = BusinessType.UPDATE)
    @RequiresPermissions("am:notification:update")
    public CommonResult<String> updateNotification(@Valid @RequestBody Notification notification){
        if(notificationService.updateNotification(notification)>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("发布系统通知")
    @PostMapping("/releaseNotification")
    @Log(title = "发布系统通知",businessType = BusinessType.UPDATE)
    @RequiresPermissions("am:notification:update")
    public CommonResult<String> releaseNotification(@RequestParam Integer notificationId){
        if(notificationService.releaseNotification(notificationId)>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("查看系统通知列表")
    @GetMapping("/getNotificationList")
    @RequiresPermissions("am:notification:query")
    public CommonResult<CommonPage<Notification>> getNotificationList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false) Integer targetType,
                                                              @RequestParam(required = false) String content,
                                                              @RequestParam(required = false) Integer status) {
        List<Notification> notificationList = notificationService.getNotificationList(pageNum,pageSize,targetType,content,status);
        if(notificationList.size()>0){
            return CommonResult.success(CommonPage.restPage(notificationList),"查看系统通知列表成功");
        }
        return CommonResult.failed("系统通知列表不存在");
    }

    @ApiOperation("查看系统通知详情")
    @GetMapping("/getNotificationDetail")
    @RequiresPermissions("am:notification:query")
    public CommonResult getNotificationDetail(@RequestParam Integer notificationId) {
        Notification notification = notificationService.getNotificationDetail(notificationId);
        if(notification != null) return CommonResult.success(notification);
        return CommonResult.failed("系统通知不存在");
    }

}
